<template>
  <div class="course-card" @click="$router.push(`/course/${course.id}`)">
    <div class="course-image">
      <img :src="course.image || 'https://via.placeholder.com/280x160'" :alt="course.title" />
      <div class="course-level">{{ course.level }}</div>
    </div>
    
    <div class="course-content">
      <h3 class="course-title">{{ course.title }}</h3>
      <p class="course-desc">{{ course.description }}</p>
      
      <div class="course-meta">
        <span class="course-duration">
          <el-icon><Clock /></el-icon>
          {{ course.duration }}小时
        </span>
        <span class="course-students">
          <el-icon><User /></el-icon>
          {{ course.students }}人学习
        </span>
      </div>
      
      <div class="course-footer">
        <div class="course-price">
          <span class="price">¥{{ course.price }}</span>
          <span class="original-price" v-if="course.originalPrice">
            ¥{{ course.originalPrice }}
          </span>
        </div>
        <el-button type="primary" size="small" @click.stop="handleBuy">
          立即购买
        </el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { defineProps } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/store/user'
import { ElMessage } from 'element-plus'

const props = defineProps({
  course: {
    type: Object,
    required: true
  }
})

const router = useRouter()
const userStore = useUserStore()

const handleBuy = () => {
  if (!userStore.isLoggedIn) {
    ElMessage.warning('请先登录')
    router.push('/login')
    return
  }
  
  // 这里可以添加购买逻辑
  ElMessage.success('已添加到购物车')
}
</script>

<style scoped>
.course-card {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  transition: all 0.3s;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.course-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}

.course-image {
  position: relative;
  height: 160px;
}

.course-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.course-level {
  position: absolute;
  top: 10px;
  right: 10px;
  background: rgba(64, 158, 255, 0.9);
  color: white;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
}

.course-content {
  padding: 16px;
}

.course-title {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 8px;
  color: #333;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.course-desc {
  font-size: 14px;
  color: #666;
  margin-bottom: 12px;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.course-meta {
  display: flex;
  gap: 16px;
  margin-bottom: 12px;
  font-size: 13px;
  color: #999;
}

.course-meta span {
  display: flex;
  align-items: center;
  gap: 4px;
}

.course-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.course-price {
  display: flex;
  align-items: center;
  gap: 8px;
}

.price {
  font-size: 18px;
  font-weight: bold;
  color: #f56c6c;
}

.original-price {
  font-size: 14px;
  color: #999;
  text-decoration: line-through;
}
</style>
